<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- /头像区域 -->
      <!-- 登陆表单区域 -->
      <el-form
        ref="loginFormRef"
        label-width="0px"
        class="login_form"
        :model="form"
        :rules="loginFormRules"
      >
        <!-- 用户名 -->
        <el-form-item prop="email">
          <el-input prefix-icon="iconfont icon-user" v-model="form.email"></el-input>
          <!-- 密码 -->
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" v-model="form.password" type="password"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登陆</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- /陆表单区域 -->
    </div>
  </div>
</template>
<script>
/* eslint-disable */
export default {
  data () {
    var checkEmail = (rule, value, cb) => {
      // 验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
      if (regEmail.test(value)) {
        // 合法的邮箱
        return cb()
      }

      cb(new Error('请输入合法的邮箱'))
    }
    return {
      form: {
        email: '111@qq.com',
        password: '111'
      },
      loginFormRules: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登陆密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在3到15个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮，重置登陆表单
    resetLoginForm () {
      // this.$refs存放 了该组件中注册过Ref的对象，resetFields（）是表单对象的方法，详情请看elementui官网
      this.$refs.loginFormRef.resetFields()
    },
    //点击登陆按钮
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return
        }
        // 将data从接口的返回值中结构出来，别名为res，起了别名后，data将不能使用
        //const { data: res } = await this.$http.post("api/login", this.form);

        var params = new URLSearchParams()
        params.append('email', this.form.email)
        params.append('password', this.form.password)
        const { data: res } = await this.$http({
          method: 'post',
          url: 'api/login',
          data: params,
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
        console.log(res)
        //  将token保存到sessionStorage中
        window.sessionStorage.setItem('token', res.token)
        //console.log("'" + res + "'");

        if (res.token == 'no') return this.$message.error('登陆失败')
        this.$message.success('登陆成功')

        //页面跳转
        if (res.role == 'admin')
          this.$router.push('/home')
        else if (res.role == 'normal')
          this.$router.push('/index')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  /*display: flex;
  justify-content: center;
  align-items: center;*/
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eeeeee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 5px #dddddd;
  position: absolute;
  left: 50%;
  top: 0;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eeeeee;
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  padding: 0 20px;
  width: 100%;
  box-sizing: border-box;
}
</style>

